﻿
using System.Data.Linq.Mapping;
using System.Xml.Linq;

namespace EbbinghausWord.Entities
{
    /// <summary>
    /// 书籍与单词的对应表
    /// </summary>
    [Table]
    public class TableBookWordEntity : BaseTableEntity
    {
        private int _id;

        /// <summary>
        /// 主键编号
        /// </summary>
        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL IDENTITY",
           CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int Id
        {
            get { return _id; }
            set
            {
                if (_id == value) return;

                NotifyPropertyChanging("Id");
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }


        private int _bookId;

        /// <summary>
        /// 书籍Id
        /// </summary>  
        [Column]
        public int BookId
        {
            get { return _bookId; }
            set
            {
                if (_bookId == value) return;

                NotifyPropertyChanging("BookId");
                _bookId = value;
                NotifyPropertyChanged("BookId");
            }
        }

        private string _english;

        /// <summary>
        /// 单词英文
        /// </summary>
        [Column]
        public string English
        {
            get { return _english; }
            set
            {
                if (_english == value) return;

                NotifyPropertyChanging("English");
                _english = value;
                NotifyPropertyChanged("English");
            }
        }

        /// <summary>
        /// 从Element生成实体
        /// </summary>
        /// <param name="elementString"></param>
        /// <returns></returns>
        public static TableBookWordEntity FromXElemnt(XElement element)
        {
            return new TableBookWordEntity()
                       {
                           English = element.Element("E").Value,
                           BookId = int.Parse(element.Element("B").Value),
                       };
        }

        /// <summary>
        /// 从Element生成实体
        /// </summary>
        /// <param name="elementString"></param>
        /// <returns></returns>
        public static TableBookWordEntity FromXElemnt(XElement element, int bookId)
        {
            return new TableBookWordEntity()
            {
                English = element.Value,
                BookId = bookId,
            };
        }
    }
}
